//
//  WSBannerView.m
//  WisdomStar
//
//  Created by 谭飞 on 2018/11/8.
//  Copyright © 2018年 zhangliwen. All rights reserved.
//

#import "WSBannerView.h"
#import "TYCyclePagerView.h"
#import "TYPageControl.h"
#import "WSBannerCell.h"

#import "WSBannerInfo.h"

@interface WSBannerView ()<TYCyclePagerViewDataSource, TYCyclePagerViewDelegate>

@property (nonatomic, strong) TYCyclePagerView *pageView;
@property (nonatomic, strong) TYPageControl *pageControl;

@end

@implementation WSBannerView

- (void)awakeFromNib {
    [super awakeFromNib];
    
    [self setupSubViews];
}

- (instancetype)initWithFrame:(CGRect)frame {
    if (self = [super initWithFrame:frame]) {
        [self setupSubViews];
    }
    return self;
}

- (void)setupSubViews {
    
    TYCyclePagerView *cyclePageView = [[TYCyclePagerView alloc] init];
    self.pageView = cyclePageView;
    cyclePageView.autoScrollInterval = 5.f;
    cyclePageView.delegate = self;
    cyclePageView.dataSource = self;
    [self addSubview:cyclePageView];
    [cyclePageView registerClass:[WSBannerCell class] forCellWithReuseIdentifier:@"pageViewCell"];
    
    TYPageControl *pageControl = [[TYPageControl alloc] init];
    self.pageControl = pageControl;
    pageControl.currentPageIndicatorSize = CGSizeMake(18.f, 8.f);
    pageControl.pageIndicatorSize = CGSizeMake(8.f, 8.f);
    pageControl.currentPageIndicatorTintColor = kThemeGreenColor;
    pageControl.pageIndicatorTintColor = kCommonWhiteBg;
    pageControl.hidesForSinglePage = YES;
    [self addSubview:pageControl];
    
    [cyclePageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo(self);
    }];
    
    [pageControl mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.offset(0.f);
        make.bottom.offset(-10.f);
        make.height.offset(20.f);
    }];
}


#pragma mark - TYCyclePagerViewDataSource, TYCyclePagerViewDelegate

- (NSInteger)numberOfItemsInPagerView:(TYCyclePagerView *)pageView {
    return self.datas.count;
}

- (UICollectionViewCell *)pagerView:(TYCyclePagerView *)pagerView cellForItemAtIndex:(NSInteger)index {
    WSBannerCell *cell = [pagerView dequeueReusableCellWithReuseIdentifier:@"pageViewCell" forIndex:index];
    [cell.imageView imageWithURL:self.datas[index].ad_url defaultImage:kPlaceHolderImage];
    return cell;
}

- (TYCyclePagerViewLayout *)layoutForPagerView:(TYCyclePagerView *)pageView {
    TYCyclePagerViewLayout *layout = [[TYCyclePagerViewLayout alloc] init];
    layout.itemSize = CGSizeMake(pageView.xmg_width, pageView.xmg_height);
    layout.itemSpacing = 0.f;
    layout.itemHorizontalCenter = YES;
    return layout;
}

- (void)pagerView:(TYCyclePagerView *)pageView didScrollFromIndex:(NSInteger)fromIndex toIndex:(NSInteger)toIndex {
    self.pageControl.currentPage = toIndex;
}

#pragma mark - Setter

- (void)setDatas:(NSArray<WSBannerInfo *> *)datas {
    _datas = datas;
    self.pageControl.numberOfPages = datas.count;
    self.pageView.isInfiniteLoop = datas.count != 1;
    [self.pageView reloadData];
}

@end
